package scream.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import scream.excepcion.DAOExcepcion;
import scream.modelo.Rol;
//import scream.modelo.TipoIntervencion;
import scream.util.ConexionBD;

public class RolDAO {
	public Collection<Rol> buscarPorNombre(String nombre) throws DAOExcepcion {
		System.out.println("coRolDAO: buscarPorCodigo(String codigo)");
		String query = "SELECT coRol, noRol FROM rol WHERE noRol like ?";
		
		Collection<Rol> lista = new ArrayList<Rol>();
		Connection con = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		try {
			con = ConexionBD.obtenerConexion();
			stmt = con.prepareStatement(query);
			stmt.setString(1, "%" + nombre + "%");
			rs = stmt.executeQuery();
			while (rs.next()) {
				Rol vo = new Rol();
				vo.setcoRol(rs.getString("coRol"));
				vo.setnoRol(rs.getString("noRol"));
				lista.add(vo);
			}
		} catch (SQLException e) {
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		} finally {
			this.cerrarResultSet(rs);
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
		return lista;
	}

	
	private void cerrarConexion(Connection con) {
		// TODO Auto-generated method stub
		
	}


	private void cerrarStatement(PreparedStatement stmt) {
		// TODO Auto-generated method stub
		
	}


	private void cerrarResultSet(ResultSet rs) {
		// TODO Auto-generated method stub
		
	}

	
	public void insertar(Rol vo) throws DAOExcepcion {
			System.out.println("RolDAO: insertar(Rol vo)");
			String query = "INSERT INTO rol(coRol, noRol) VALUES (?,?)";
			
			//if (findcoRol(vo.getcoRol()) !=null ) {			
			//	throw new DAOExcepcion("Codigo de Rol ya existe.");}
			//	else {System.out.print("Codigo de Rol fue creado satisfactoriamente...");}
			
			
			
			Connection con = null;
			PreparedStatement stmt = null;
			try {
				con = ConexionBD.obtenerConexion();
				stmt = con.prepareStatement(query);
				stmt.setString(1, vo.getcoRol());
				stmt.setString(2, vo.getnoRol());
				int i = stmt.executeUpdate();
				if (i != 1) {
					throw new SQLException("No se pudo insertar");
				}
			} catch (SQLException e) {
				System.err.println(e.getMessage());
				throw new DAOExcepcion(e.getMessage());
			} finally {
				this.cerrarStatement(stmt);
				this.cerrarConexion(con);
			}
		}

		public Rol obtener(String coRol) throws DAOExcepcion {
			System.out.println("coRolDAO: obtener(String coRol)");
			Rol vo = new Rol();
			Connection con = null;
			PreparedStatement stmt = null;
			ResultSet rs = null;
			try {
				String query = "select coRol, noRol from rol where coRol=?";
				con = ConexionBD.obtenerConexion();
				stmt = con.prepareStatement(query);
				stmt.setString(1, coRol);
				rs = stmt.executeQuery();
				if (rs.next()) {
					vo.setcoRol(rs.getString(1));
					vo.setnoRol(rs.getString(2));
				}
			} catch (SQLException e) {
				System.err.println(e.getMessage());
				throw new DAOExcepcion(e.getMessage());
			} finally {
				this.cerrarResultSet(rs);
				this.cerrarStatement(stmt);
				this.cerrarConexion(con);
			}
			return vo;
		}

		public void eliminar(String rolNombre) throws DAOExcepcion {
			System.out.println("coRolDAO: eliminar(String coRol)");
			String query = "DELETE FROM coRol WHERE coRol=?";
			Connection con = null;
			PreparedStatement stmt = null;
			try {
				con = ConexionBD.obtenerConexion();
				stmt = con.prepareStatement(query);
				stmt.setString(1, rolNombre);
				int i = stmt.executeUpdate();
				if (i != 1) {
					throw new SQLException("No se pudo eliminar");
				}
			} catch (SQLException e) {
				System.err.println(e.getMessage());
				throw new DAOExcepcion(e.getMessage());
			} finally {
				this.cerrarStatement(stmt);
				this.cerrarConexion(con);
			}
		}

		public void actualizar(Rol vo) throws DAOExcepcion {
			System.out.println("RolDAO: actualizar(Rol vo)");
			String query = "update rol set noRol=? where noRol=?";
			Connection con = null;
			PreparedStatement stmt = null;
			try {
				con = ConexionBD.obtenerConexion();
				stmt = con.prepareStatement(query);
				stmt.setString(1, vo.getnoRol());
				stmt.setString(2, vo.getcoRol());
				int i = stmt.executeUpdate();
				if (i != 1) {
					throw new SQLException("No se pudo actualizar");
				}
			} catch (SQLException e) {
				System.err.println(e.getMessage());
				throw new DAOExcepcion(e.getMessage());
			} finally {
				this.cerrarStatement(stmt);
				this.cerrarConexion(con);
			}
		}

		
		public Collection<Rol> listar() throws DAOExcepcion {
				System.out.println("RolDAO: listar()");
				Collection<Rol> c = new ArrayList<Rol>();
				Connection con = null;
				PreparedStatement stmt = null;
				ResultSet rs = null;
				try {
					con = ConexionBD.obtenerConexion();
					String query = "SELECT coRol,noRol from rol";
					stmt = con.prepareStatement(query);
					rs = stmt.executeQuery();
					while (rs.next()) {
						Rol vo = new Rol();
						vo.setcoRol(rs.getString("coRol"));
						vo.setnoRol(rs.getString("noRol"));
						c.add(vo);
					}
				} catch (SQLException e) {
					System.err.println(e.getMessage());
					throw new DAOExcepcion(e.getMessage());
				} finally {
					this.cerrarStatement(stmt);
					this.cerrarConexion(con);
				}
				return c;
			}

		public Rol findcoRol(String CoRol) throws DAOExcepcion {
			System.out.println("RolDAO: findRol(String CoRol)");
			Rol oRol = null;
			Connection con = null;
			PreparedStatement stmt = null;
			ResultSet rs = null;
			try {
				String query = "SELECT * FROM ROL WHERE CoRol = ?";
				con = ConexionBD.obtenerConexion();
				stmt = con.prepareStatement(query);
				stmt.setString(1, CoRol);
				rs = stmt.executeQuery();
				if (rs.next()) {
					oRol = new Rol();
					oRol.setcoRol(rs.getString("CoRol"));
					//oRol.setnoRol(rs.getString("NoRol"));
				}
			} catch (SQLException e) {
				throw new DAOExcepcion(e.getMessage());
			} finally {
				this.cerrarResultSet(rs);
				this.cerrarStatement(stmt);
				this.cerrarConexion(con);
			}
			return oRol;
		}	

}
